package fr.imag.ufrima.ctte.gui.views;

import java.awt.BorderLayout;
import java.util.Iterator;

import javax.swing.GroupLayout;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeSelectionModel;

import fr.imag.ufrima.ctte.ImagCTTE;
import fr.imag.ufrima.ctte.Task;
import fr.imag.ufrima.ctte.TaskDiagram;
import fr.imag.ufrima.ctte.events.ImagCtteAdapter;
import fr.imag.ufrima.ctte.events.ImagCtteEvent;
import fr.imag.ufrima.ctte.events.TaskDiagramAdapter;
import fr.imag.ufrima.ctte.events.TaskDiagramEvent;
import fr.imag.ufrima.ctte.gui.editors.TaskDiagramDesignPanel;

public class TaskTreeSubView extends SubView {

	/**
	 * Constructor
	 */
	public TaskTreeSubView(ImagCTTE imagCTTE){
		super(imagCTTE,"Tasks Tree");
		InitComponents();
		taskTree.getSelectionModel().setSelectionMode
        (TreeSelectionModel.SINGLE_TREE_SELECTION);
		taskTree.setExpandsSelectedPaths(true);
		taskTree.addTreeSelectionListener(new TreeSelectionListener(){			
			public void valueChanged(TreeSelectionEvent arg0) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                taskTree.getLastSelectedPathComponent();

				if (node == null)
				//Nothing is selected.	
				return;
				
				Task task = (Task)node.getUserObject();
				getImagCTTE().getCurrentTaskDiagram().setSelectedTask(task);
			}
			
		});
		
		
		imagCTTE.addImagCtteListener(new ImagCtteAdapter(){			
			public void taskDiagramSwitched(ImagCtteEvent event) {				
				TaskDiagram taskDiagram = event.getTaskDiagram();
				if (taskDiagram != null){		
					
					createTree();
					taskDiagram.addTaskDiagramListener(new TaskDiagramAdapter(){
						
						public void taskAdded(TaskDiagramEvent event) {
							createTree();
						}
						
						public void taskPropertiesChanged(TaskDiagramEvent event) {
							taskTree.repaint();
						}
						public void taskRemoved(TaskDiagramEvent event) {
							createTree();
 						}
					});
				} else {
					// no task diagram
					// TODO remove all items
				}
			}
		});
	}
	private void createTree(){
		TaskDiagram taskDiagram = getImagCTTE().getCurrentTaskDiagram();
		DefaultMutableTreeNode top =
	        new DefaultMutableTreeNode(taskDiagram.getRootTask());
		createNodes(top);		
		TreeModel treeModel = new DefaultTreeModel(top);
		taskTree.setModel(treeModel);
		expandAll(taskTree);
	}
	private void createNodes(DefaultMutableTreeNode top) {
		System.err.println(": " + top);
		Task t = (Task)top.getUserObject();
		Iterator<Task> it = t.getSubTasks().iterator();
		while(it.hasNext()){
			Task tt = (Task) it.next();
			DefaultMutableTreeNode node = new DefaultMutableTreeNode(tt);
			top.add(node);
			createNodes(node);
		}
		
	}
	public void expandAll(JTree tree) {
	    int row = 0;
	    while (row < tree.getRowCount()) {
	      tree.expandRow(row);
	      row++;
	      }
	    }
	/**
	 * Initialize componenets
	 */
	protected void InitComponents(){
        scrollPane = new javax.swing.JScrollPane();
        taskTree = new javax.swing.JTree();

        scrollPane.setViewportView(taskTree);
        
        this.setLayout(new BorderLayout());
        this.add(scrollPane, BorderLayout.CENTER);
	}
	/**
	 * Components
	 */
	private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTree taskTree;
}
